package com.kbbywyclient.di;

import com.kbbywyclient.app.API;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.util.LogUtil;
import com.kbbywyclient.util.customservice.CustomGsonConverterFactory;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;

/**
 * Created by FHW
 * on 2017/3/21
 */

@Module
public class NetworkModule {

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .addInterceptor(new HttpLoggingInterceptor(message -> LogUtil.d(message)).setLevel(HttpLoggingInterceptor.Level.BASIC))//打印请求接口
                .build();
        return okHttpClient;
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient client) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.SERVER_ADDRESS)
                .addConverterFactory(CustomGsonConverterFactory.create())// 添加自定义转换器
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())// 添加Rx适配器
                .client(client).build();
        return retrofit;
    }

    //
    @Provides
    @Singleton
    ApiService provideApiService(Retrofit retrofit) {
        return retrofit.create(ApiService.class);
    }

}
